import tensorflow as tf
import numpy as np

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 生成一个一百个元素的数组
x_data = np.random.rand(100).astype(np.float32)

# 得到一个线性函数
y_data = x_data * 0.1 + 0.3

x = tf.Variable(0.)
b = tf.Variable(0.)

y = x * x_data + b

# 二次代价函数 得到方差最小值 reduce_mean
lose = tf.reduce_mean(tf.square(y - y_data))
print("------------w , b 的引用在内部train n次，但只初始化一次-------------------")

# 梯度下降优化器   步进0.1
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(lose)

# 变量初始化
init = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(init)
    for i in range(1, 201):
        session.run(train)
        if i % 20 == 0:
            print(session.run([x, b]))
